package com.baseweb.webadmin.core.model.dao.impl;

import com.baseweb.webadmin.core.model.bvo.*;
import com.baseweb.webadmin.core.model.dao.UserDao;
import com.baseweb.webadmin.core.model.mapper.*;
import com.baseweb.webadmin.core.model.pojo.*;
import com.baseweb.webadmin.core.util.ObjectFormatter;
import com.baseweb.webadmin.core.util.RequestPage;
import com.baseweb.webadmin.core.util.StringUtil;
import org.apache.commons.lang.StringUtils;
import org.mybatis.spring.support.SqlSessionDaoSupport;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class UserDaoImpl extends SqlSessionDaoSupport implements UserDao {

    @Autowired
    UserMapper userMapper;

    @Autowired
    ByUserBizAuthMapper byUserBizAuthMapper;

    @Autowired
    ByUserBizPackageMapper byUserBizPackageMapper;

    @Autowired
    ByUserBizPackageDetailMapper byUserBizPackageDetailMapper;

    @Autowired
    ByUserBizProductMapper byUserBizProductMapper;

    @Autowired
    ByOpenCityMapper byOpenCityMapper;

    @Autowired
    ByUserTrolleyMapper byUserTrolleyMapper;

    @Autowired
    ByUserInviteMapper byUserInviteMapper;

    @Autowired
    ByUserWedBudgetMapper byUserWedBudgetMapper;

    @Autowired
    ByUserExampleMapper byUserExampleMapper;

    @Autowired
    ByUserBizAgentCityMapper byUserBizAgentCityMapper;

    @Autowired
    ByUserExampleDetailMapper byUserExampleDetailMapper;

    @Autowired
    ByUserRecommendMapper byUserRecommendMapper;

    @Autowired
    ByExampleBudgetMapper byExampleBudgetMapper;

    @Autowired
    ByExampleDiscussMapper byExampleDiscussMapper;

    @Autowired
    DictMapper dictMapper;

    @Autowired
    ByUserMessageMapper userMessageMapper;

    @Override
    public User selectUserById(Integer userid) {
        return userMapper.selectByPrimaryKey(userid);
    }

    @Override
    public User selectUserByUserName(String username) {
        return this.getSqlSession().selectOne("com.baseweb.webadmin.core.model.dao.UserDao.selectUserByUserName", username);
    }

    @Override
    public User selectUserByToken(String token) {
        return this.getSqlSession().selectOne("com.baseweb.webadmin.core.model.dao.UserDao.selectUserByToken", token);
    }

    @Override
    public List<User> selectUsers(String username) {
        return this.getSqlSession().selectOne("com.baseweb.webadmin.core.model.dao.UserDao.selectUsers", username);
    }

    @Override
    public List<User> selectUserByIds(List<Integer> ids) {
        UserExample example = new UserExample();
        UserExample.Criteria criteria = example.createCriteria();
        if(ids != null && ids.size() > 0){
            criteria.andIdIn(ids);
        }else{
            return null;
        }
        example.setOrderByClause(" id desc ");
        return userMapper.selectByExample(example);
    }

    @Override
    public List<UserRoleBvo> selectUsersByPage(Map map) {
//        if (pageNo < 1) {
//            pageNo = 1;
//        }
//
//        if (pageSize < 1) {
//            pageSize = 20;
//        }
//
//        int startCount = (pageNo - 1) * pageSize;
//        map.put("startCount", startCount);
//        map.put("pageSize", pageSize);

        return getSqlSession().selectList("com.baseweb.webadmin.core.model.dao.UserDao.selectUsersByPage", map);
    }

    @Override
    public Integer selectUsersByNameRows(Map map) {
        return (Integer) getSqlSession().selectOne("com.baseweb.webadmin.core.model.dao.UserDao.selectUsersByNameRows", map);
    }

    @Override
    public List<User> selectUserByUserType(Integer userType, Integer status) {
        UserExample example = new UserExample();
        UserExample.Criteria criteria = example.createCriteria();
        if(userType != null){
            criteria.andUserTypeEqualTo(userType);
        }
        if(status != null){
            criteria.andStatusEqualTo(status);
        }
        return userMapper.selectByExample(example);
    }

    @Override
    public List<User> selectAgentUserList(Integer agentId, Integer status, String cn_name, String username, String identity, Integer start, Integer offset) {
        UserExample example = new UserExample();
        UserExample.Criteria criteria = example.createCriteria();
        if(agentId != null){
            criteria.andBizAgentIdEqualTo(agentId);
        }
        if(status != null){
            criteria.andStatusEqualTo(status);
        }
        if(cn_name != null){
            criteria.andCnNameEqualTo(cn_name);
        }
        if(username != null){
            criteria.andUsernameEqualTo(username);
        }
        if(identity != null){
            criteria.andIdentityEqualTo(identity);
        }
        example.setOrderByClause(String.format("id desc limit %s,%s", start, offset));
        return userMapper.selectByExample(example);
    }

    @Override
    public Integer selectAgentUserListCount(Integer agentId, Integer status, String cn_name, String username, String identity) {
        UserExample example = new UserExample();
        UserExample.Criteria criteria = example.createCriteria();
        if(agentId != null){
            criteria.andBizAgentIdEqualTo(agentId);
        }
        if(status != null){
            criteria.andStatusEqualTo(status);
        }
        if(cn_name != null){
            criteria.andCnNameEqualTo(cn_name);
        }
        if(username != null){
            criteria.andUsernameEqualTo(username);
        }
        if(identity != null){
            criteria.andIdentityEqualTo(identity);
        }
        return userMapper.countByExample(example);
    }

    @Override
    public User selectUserByWxId(String openId, String unionId) {
        UserExample example = new UserExample();
        UserExample.Criteria criteria = example.createCriteria();
        if(openId != null){
            criteria.andWxOpenidEqualTo(openId);
        }
        if(unionId != null){
            criteria.andWxUnionidEqualTo(unionId);
        }
        List<User> userList = userMapper.selectByExample(example);

        if (userList != null && userList.size() > 0){
            return userList.get(0);
        }
        return null;
    }

    /**
     * 根据邮箱在user表中判断该邮箱
     * 是否已经被使用
     *
     * @param email
     * @return
     */
    @Override
    public List<User> selectUserByEmail(String email) {
        return this.getSqlSession().selectList("com.baseweb.webadmin.core.model.dao.UserDao.selectUserByEmail", email);
    }

    @Override
    public List<User> selectUserListByPage(Integer id, String username, Integer start, Integer offset) {
        UserExample example = new UserExample();
        UserExample.Criteria criteria = example.createCriteria();
        if(id != null){
            criteria.andIdEqualTo(id);
        }
        if(StringUtils.isNotBlank(username)){
            criteria.andUsernameEqualTo(username);
        }
        example.setOrderByClause(String.format("id desc limit %s,%s", start, offset));
        return userMapper.selectByExample(example);
    }

    @Override
    public Integer selectUserListCount(Integer id, String username) {
        UserExample example = new UserExample();
        UserExample.Criteria criteria = example.createCriteria();
        if(id != null){
            criteria.andIdEqualTo(id);
        }
        if(StringUtils.isNotBlank(username)){
            criteria.andUsernameEqualTo(username);
        }
        return userMapper.countByExample(example);
    }

    @Override
    public List<User> selectUserListByTypeAndPage(Integer user_type, Integer start, Integer offset) {
        UserExample example = new UserExample();
        UserExample.Criteria criteria = example.createCriteria();
        if(user_type != null){
            criteria.andUserTypeEqualTo(user_type);
        }
        example.setOrderByClause(String.format("id desc limit %s,%s", start, offset));
        return userMapper.selectByExample(example);
    }

    @Override
    public Integer selectUserListByTypeCount(Integer user_type) {
        UserExample example = new UserExample();
        UserExample.Criteria criteria = example.createCriteria();
        if(user_type != null){
            criteria.andUserTypeEqualTo(user_type);
        }
        return userMapper.countByExample(example);
    }

    @Override
    public Integer insertBizVerify(ByUserBizAuth byUserBizAuth) {
        if (byUserBizAuth.getId() !=null && byUserBizAuth.getId() > 0){
            return byUserBizAuthMapper.updateByPrimaryKey(byUserBizAuth);
        }else {
            return byUserBizAuthMapper.insert(byUserBizAuth);
        }
    }

    @Override
    public ByUserBizAuth findBizVerify(Integer id) {
        return byUserBizAuthMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<ByUserBizAuth> selectBizVerifyListByPage(Integer userId, Integer authType, String name, Integer start, Integer offset, Integer status) {
        ByUserBizAuthExample example = new ByUserBizAuthExample();
        ByUserBizAuthExample.Criteria criteria = example.createCriteria();
        if(userId != null){
            criteria.andUserIdEqualTo(userId);
        }
        if(authType != null){
            criteria.andAuthTypeEqualTo(authType);
        }
        if(name != null){
            criteria.andNameEqualTo(name);
        }
        if(status != null){
            criteria.andStatusEqualTo(status);
        }
        example.setOrderByClause(String.format("status asc limit %s,%s", start, offset));
        return byUserBizAuthMapper.selectByExample(example);
    }
    @Override
    public List<ByUserBizAuthvo> selectBizVerifyListByPage1(ByUserBizAuthvo auth, Integer start, Integer offset) {
        return byUserBizAuthMapper.selectByUserBizAuthListPage(auth, start, offset);
    }
    @Override
    public Integer selectBizVerifyListCount1(ByUserBizAuthvo authBvo) {
        return byUserBizAuthMapper.selectByUserBizAuthListCount(authBvo);
    }
    @Override
    public Integer selectBizVerifyListCount(Integer userId, Integer authType, String name, Integer status) {
        ByUserBizAuthExample example = new ByUserBizAuthExample();
        ByUserBizAuthExample.Criteria criteria = example.createCriteria();
        if(userId != null){
            criteria.andUserIdEqualTo(userId);
        }
        if(authType != null){
            criteria.andAuthTypeEqualTo(authType);
        }
        if(name != null){
            criteria.andNameEqualTo(name);
        }
        if(status != null){
            criteria.andStatusEqualTo(status);
        }
        return byUserBizAuthMapper.countByExample(example);
    }

    @Override
    public List<UserVerifyBvo> selectBizVerifyStatusList(List<Integer> userId, Integer status) {
        Map map = new HashMap();
        map.put("userIds", userId);
        map.put("status", status);
        return this.getSqlSession().selectList("com.baseweb.webadmin.core.model.dao.UserDao.selectBizVerifyStatusList", map);
    }
    @Override
    public List<ByUserBizAuth> selectBizVerifyStatusList1(Integer userId) {
        return byUserBizAuthMapper.selectByUserBizAuthByUserIdAndStatus(userId);
    }
    @Override
    public List<ByUserBizPackage> selectPackageListByPage(Integer userId, String name, Integer start, Integer offset) {
        ByUserBizPackageExample example = new ByUserBizPackageExample();
        ByUserBizPackageExample.Criteria criteria = example.createCriteria();
        if(userId != null){
            criteria.andUserIdEqualTo(userId);
        }
        if(name != null){
            criteria.andNameEqualTo(name);
        }
        example.setOrderByClause(String.format("id desc limit %s,%s", start, offset));
        return byUserBizPackageMapper.selectByExample(example);
    }

    @Override
    public Integer selectPackageListCount(Integer userId, String name) {
        ByUserBizPackageExample example = new ByUserBizPackageExample();
        ByUserBizPackageExample.Criteria criteria = example.createCriteria();
        if(userId != null){
            criteria.andUserIdEqualTo(userId);
        }
        if(name != null){
            criteria.andNameEqualTo(name);
        }
        return byUserBizPackageMapper.countByExample(example);
    }

    @Override
    public List<ByUserBizPackageDetail> selectPackageDetailListByPage(Integer userId, Integer packageId, Integer productId, Integer start, Integer offset) {
        ByUserBizPackageDetailExample example = new ByUserBizPackageDetailExample();
        ByUserBizPackageDetailExample.Criteria criteria = example.createCriteria();
        if(userId != null){
            criteria.andUserIdEqualTo(userId);
        }
        if(packageId != null){
            criteria.andPackageIdEqualTo(packageId);
        }
        if(productId != null){
            criteria.andProductIdEqualTo(productId);
        }
        example.setOrderByClause(String.format("id desc limit %s,%s", start, offset));
        return byUserBizPackageDetailMapper.selectByExample(example);
    }

    @Override
    public Integer selectPackageDetailListCount(Integer userId, Integer packageId, Integer productId) {
        ByUserBizPackageDetailExample example = new ByUserBizPackageDetailExample();
        ByUserBizPackageDetailExample.Criteria criteria = example.createCriteria();
        if(userId != null){
            criteria.andUserIdEqualTo(userId);
        }
        if(packageId != null){
            criteria.andPackageIdEqualTo(packageId);
        }
        if(productId != null){
            criteria.andProductIdEqualTo(productId);
        }
        return byUserBizPackageDetailMapper.countByExample(example);
    }

    @Override
    public List<ByUserBizProduct> selectProductListByPage(Integer userId, Integer productType, Integer channelId, Integer start, Integer offset,
                                                          String sortBy, String styleType, String price, String keyword, Integer status) {
        ByUserBizProductExample example = new ByUserBizProductExample();
        ByUserBizProductExample.Criteria criteria = example.createCriteria();
        if(userId != null){
            criteria.andUserIdEqualTo(userId);
        }
        if(productType != null) {
            if(productType == 3){
                List<Integer> list = new ArrayList<Integer>();
                list.add(1);
                list.add(2);
                criteria.andProductTypeIn(list);
            }else{
                criteria.andProductTypeEqualTo(productType);
            }
        }
        if(channelId != null){
            criteria.andChannelIdEqualTo(channelId);
        }
        if (status != null){
            if (status == 0) { // 如果传0，代表查询未删除的
                criteria.andStatusNotEqualTo(2);
            } else {
                criteria.andStatusEqualTo(status);
            }
        }
        if (keyword != null){
            criteria.andNameLike("%" + keyword + "%");
        }
        example.setOrderByClause(String.format("current_price desc limit %s,%s", start, offset));
        return byUserBizProductMapper.selectByExample(example);
    }

    @Override
    public List<ByUserBizProductBvo> selectProductVoListByPage(Integer userId, String userName, String userMobile, String userChannel, String keyword,
                                                               Integer auditType, Integer status, String startTime, String endTime,
                                                               String province, String city, String county, Integer start, Integer offset) {
        return byUserBizProductMapper.selectProductVoListByPage(userId, userName, userMobile, userChannel, keyword, auditType, status, startTime, endTime, province, city, county, start, offset);
    }

    public Integer selectProductListCount(String userName, String userMobile, String userChannel, String keyword,
                                          Integer auditType, Integer status, String startTime, String endTime,
                                          String province, String city, String county) {
        return byUserBizProductMapper.selectProductVoCount(null, userName, userMobile, userChannel, keyword, auditType, status, startTime, endTime, province, city, county);
    }

    @Override
    public List<ByUserBizProductBvo> selectProductVoListByPage(String userName, String userMobile, Integer userChannel, String keyword,
                                                               Integer auditType, Integer status, String startTime, String endTime,
                                                               String province, String city, String county, Integer start, Integer offset) {
        String sql = "";
        if (userName != null && !"".equals(userName)) {
            sql += " and u.cn_name like '%" + userName + "%'";
        }
        if (userMobile != null && !"".equals(userMobile)) {
            sql += " and u.mobile = '" + userMobile + "'";
        }
        if (userChannel != null && userChannel != 0) {
            sql += " and u.biz_work_type = " + userChannel;
        }
        if (keyword != null && !"".equals(keyword)) {
            sql += " and u.name LIKE '%" + keyword + "%'";
        }
        User user = new User();
        user.setUsername(sql);
        return byUserBizProductMapper.selectProductVoListBySQL(user);
    }

    @Override
    public Integer selectProductListCount(Integer userId, Integer productType, Integer channelId,
                                          String sortBy, String styleType, String price, String keyword, Integer status) {
        ByUserBizProductExample example = new ByUserBizProductExample();
        ByUserBizProductExample.Criteria criteria = example.createCriteria();
        if(userId != null){
            criteria.andUserIdEqualTo(userId);
        }
        if(productType != null){
            criteria.andProductTypeEqualTo(productType);
        }
        if(channelId != null){
            criteria.andChannelIdEqualTo(channelId);
        }
        if (status != null){
            if (status == 0) { // 如果传0，代表查询未删除的
                criteria.andStatusNotEqualTo(2);
            } else {
                criteria.andStatusEqualTo(status);
            }
        }
        if (keyword != null) {
            criteria.andNameLike("%" + keyword + "%");
        }
        return byUserBizProductMapper.countByExample(example);
    }

    @Override
    public Integer insertBizPackage(ByUserBizPackage byUserBizPackage) {
        if (byUserBizPackage.getId() !=null && byUserBizPackage.getId() > 0){
            return byUserBizPackageMapper.updateByPrimaryKey(byUserBizPackage);
        }else {
            return byUserBizPackageMapper.insert(byUserBizPackage);
        }
    }

    @Override
    public ByUserBizPackage findBizPackage(Integer id) {
        return byUserBizPackageMapper.selectByPrimaryKey(id);
    }

    @Override
    public Integer insertBizPackageDetail(ByUserBizPackageDetail byUserBizPackageDetail) {
        if (byUserBizPackageDetail.getId() !=null && byUserBizPackageDetail.getId() > 0){
            return byUserBizPackageDetailMapper.updateByPrimaryKey(byUserBizPackageDetail);
        }else {
            return byUserBizPackageDetailMapper.insert(byUserBizPackageDetail);
        }
    }

    @Override
    public ByUserBizPackageDetail findBizPackageDetail(Integer id) {
        return byUserBizPackageDetailMapper.selectByPrimaryKey(id);
    }

    @Override
    public Integer insertBizProduct(ByUserBizProduct byUserBizProduct) {
        if (byUserBizProduct.getId() !=null && byUserBizProduct.getId() > 0){
            return byUserBizProductMapper.updateByPrimaryKey(byUserBizProduct);
        }else {
            return byUserBizProductMapper.insert(byUserBizProduct);
        }
    }

    @Override
    public ByUserBizProduct findBizProduct(Integer id) {
        return byUserBizProductMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<ByUserBizProduct> findBizProductList(List<Integer> ids) {
        ByUserBizProductExample example = new ByUserBizProductExample();
        ByUserBizProductExample.Criteria criteria = example.createCriteria();
        if(ids != null && ids.size() > 0){
            criteria.andIdIn(ids);
            return byUserBizProductMapper.selectByExample(example);
        }
        return null;
    }

    @Override
    public Integer insertOpenCity(ByOpenCity byOpenCity) {
        if (byOpenCity.getId() !=null && byOpenCity.getId() > 0){
            return byOpenCityMapper.updateByPrimaryKey(byOpenCity);
        }else {
            return byOpenCityMapper.insert(byOpenCity);
        }
    }

    @Override
    public ByOpenCity findOpenCity(Integer id) {
        return byOpenCityMapper.selectByPrimaryKey(id);
    }

    @Override
    public Integer deleteOpenCity(Integer id) {
        return byOpenCityMapper.deleteByPrimaryKey(id);
    }

    @Override
    public List<ByOpenCity> selectOpenCityListByPage(Integer userId, Integer openType, String province, Integer start, Integer offset) {
        ByOpenCityExample example = new ByOpenCityExample();
        ByOpenCityExample.Criteria criteria = example.createCriteria();
        if(userId != null){
            criteria.andUserIdEqualTo(userId);
        }
        if(openType != null){
            criteria.andOpenTypeEqualTo(openType);
        }
        if(province != null){
            criteria.andProvinceEqualTo(province);
        }
        example.setOrderByClause(String.format("id desc limit %s,%s", start, offset));
        return byOpenCityMapper.selectByExample(example);
    }

    @Override
    public Integer selectOpenCityListCount(Integer userId, Integer openType, String province) {
        ByOpenCityExample example = new ByOpenCityExample();
        ByOpenCityExample.Criteria criteria = example.createCriteria();
        if(userId != null){
            criteria.andUserIdEqualTo(userId);
        }
        if(openType != null){
            criteria.andOpenTypeEqualTo(openType);
        }
        if(province != null){
            criteria.andProvinceEqualTo(province);
        }
        return byOpenCityMapper.countByExample(example);
    }

    @Override
    public List<ByOpenCity> selectAllCities(Integer userId) {
        ByOpenCityExample example = new ByOpenCityExample();
        ByOpenCityExample.Criteria criteria = example.createCriteria();
        if(userId != null){
            criteria.andUserIdEqualTo(userId);
        }
        return byOpenCityMapper.selectByExample(example);
    }

    @Override
    public List<ByUserTrolley> selectTrolleyListByPage(Integer userId, Integer start, Integer offset) {
        ByUserTrolleyExample example = new ByUserTrolleyExample();
        ByUserTrolleyExample.Criteria criteria = example.createCriteria();
        if(userId != null){
            criteria.andUserIdEqualTo(userId);
        }
        example.setOrderByClause(String.format("id desc limit %s,%s", start, offset));
        return byUserTrolleyMapper.selectByExample(example);
    }

    @Override
    public Integer selectTrolleyListCount(Integer userId) {
        ByUserTrolleyExample example = new ByUserTrolleyExample();
        ByUserTrolleyExample.Criteria criteria = example.createCriteria();
        if(userId != null){
            criteria.andUserIdEqualTo(userId);
        }
        return byUserTrolleyMapper.countByExample(example);
    }

    @Override
    public Integer insertTrolley(ByUserTrolley byUserTrolley) {
        if (byUserTrolley != null){
            return byUserTrolleyMapper.insert(byUserTrolley);
//            ByUserTrolley tmp = findTrolleyByUserAndProduct(byUserTrolley.getUserId(), byUserTrolley.getProductId());
//            if (tmp == null){
//                return byUserTrolleyMapper.insert(byUserTrolley);
//            }
        }
        return null;
    }

    @Override
    public ByUserTrolley findTrolley(Integer id) {
        return byUserTrolleyMapper.selectByPrimaryKey(id);
    }

    @Override
    public ByUserTrolley findTrolleyByUserAndProduct(Integer userId, Integer productId) {
        ByUserTrolleyExample example = new ByUserTrolleyExample();
        ByUserTrolleyExample.Criteria criteria = example.createCriteria();
        if(userId != null){
            criteria.andUserIdEqualTo(userId);
        }
        if(productId != null){
            criteria.andProductIdEqualTo(productId);
        }
        List<ByUserTrolley> byUserTrolleys = byUserTrolleyMapper.selectByExample(example);
        if (byUserTrolleys != null && byUserTrolleys.size() > 0){
            return byUserTrolleys.get(0);
        }
        return null;
    }

    @Override
    public List<ByUserTrolleyBvo> selectByUserTrolleyList(Integer userId) {
        return byUserTrolleyMapper.selectByUserTrolleyList(userId);
    }

    @Override
    public List<ByUserBizProduct> selectTrolleyProductListByPage(Integer userId, Integer start, Integer offset) {
        ByUserTrolleyExample example = new ByUserTrolleyExample();
        ByUserTrolleyExample.Criteria criteria = example.createCriteria();
        if(userId != null){
            criteria.andUserIdEqualTo(userId);
        }
        example.setOrderByClause(String.format("id desc limit %s,%s", start, offset));

        List<ByUserTrolley> byUserTrolleys = byUserTrolleyMapper.selectByExample(example);


        //select products
        List<Integer> productIds = new ArrayList<Integer>();
        for(ByUserTrolley byUserTrolley : byUserTrolleys){
            productIds.add(byUserTrolley.getProductId());
        }

        ByUserBizProductExample productExample = new ByUserBizProductExample();
        ByUserBizProductExample.Criteria productCriteria = productExample.createCriteria();
        if(userId != null){
            productCriteria.andIdIn(productIds);
        }

        return byUserBizProductMapper.selectByExample(productExample);
    }

    @Override
    public Integer selectTrolleyProductListCount(Integer userId) {
        ByUserTrolleyExample example = new ByUserTrolleyExample();
        ByUserTrolleyExample.Criteria criteria = example.createCriteria();
        if(userId != null){
            criteria.andUserIdEqualTo(userId);
        }
        return byUserTrolleyMapper.countByExample(example);
    }

    @Override
    public Integer deleteTrolley(Integer id) {
        return byUserTrolleyMapper.deleteByPrimaryKey(id);
    }

    @Override
    public Integer deleteTrolleyByUserAndProduct(Integer userId, List<Integer> productIds) {
        ByUserTrolleyExample example = new ByUserTrolleyExample();
        ByUserTrolleyExample.Criteria criteria = example.createCriteria();
        if(userId != null){
            criteria.andUserIdEqualTo(userId);
        }
        if(productIds != null){
            criteria.andProductIdIn(productIds);
        }
        return byUserTrolleyMapper.deleteByExample(example);
    }

    @Override
    public Integer insertInvite(ByUserInvite byUserInvite) {
        return byUserInviteMapper.insert(byUserInvite);
    }

    @Override
    public ByUserInvite findInvite(Integer id) {
        return byUserInviteMapper.selectByPrimaryKey(id);
    }

    @Override
    public ByUserInvite findInviteBy(Integer userId) {
        ByUserInviteExample example = new ByUserInviteExample();
        ByUserInviteExample.Criteria criteria = example.createCriteria();
        if(userId != null){
            criteria.andInviteByEqualTo(userId);
        }
        List<ByUserInvite> invites = byUserInviteMapper.selectByExample(example);
        if (invites != null && invites.size() > 0)
        {
            return invites.get(0);
        }
        return null;
    }

    @Override
    public List<ByUserInvite> selectInviteListByPage(Integer userId, Integer inviteBy, Integer start, Integer offset) {
        ByUserInviteExample example = new ByUserInviteExample();
        ByUserInviteExample.Criteria criteria = example.createCriteria();
        if(userId != null){
            criteria.andInviteByEqualTo(userId);
        }
        if(inviteBy != null){
            criteria.andInviteByEqualTo(inviteBy);
        }
        example.setOrderByClause(String.format("id desc limit %s,%s", start, offset));
        return byUserInviteMapper.selectByExample(example);
    }

    @Override
    public Integer selectInviteListCount(Integer userId, Integer inviteBy) {
        ByUserInviteExample example = new ByUserInviteExample();
        ByUserInviteExample.Criteria criteria = example.createCriteria();
        if(userId != null){
            criteria.andInviteByEqualTo(userId);
        }
        if(inviteBy != null){
            criteria.andInviteByEqualTo(inviteBy);
        }
        return byUserInviteMapper.countByExample(example);
    }

    @Override
    public Integer insertBudget(ByUserWedBudget byUserWedBudget) {
        return byUserWedBudgetMapper.insert(byUserWedBudget);
    }

    @Override
    public ByUserWedBudget findBudget(Integer id) {
        return byUserWedBudgetMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<ByUserWedBudget> selectBudgetListByPage(Integer userId, Integer start, Integer offset) {
        ByUserWedBudgetExample example = new ByUserWedBudgetExample();
        ByUserWedBudgetExample.Criteria criteria = example.createCriteria();
        if(userId != null){
            criteria.andUserIdEqualTo(userId);
        }
        example.setOrderByClause(String.format("id desc limit %s,%s", start, offset));
        return byUserWedBudgetMapper.selectByExample(example);
    }

    @Override
    public Integer selectBudgetListCount(Integer userId) {
        ByUserWedBudgetExample example = new ByUserWedBudgetExample();
        ByUserWedBudgetExample.Criteria criteria = example.createCriteria();
        if(userId != null){
            criteria.andUserIdEqualTo(userId);
        }
        return byUserWedBudgetMapper.countByExample(example);
    }

    @Override
    public List<ByUserWedBudget> selectIntentCustomListByPage(ByUserWedBudget custom, Integer start, Integer offset) {
        ByUserWedBudgetExample example = new ByUserWedBudgetExample();
        ByUserWedBudgetExample.Criteria criteria = example.createCriteria();
       /* if(userId != null){
            criteria.andUserIdEqualTo(userId);
        }*/
       if(null != custom && null != (custom.getDistrictList())){
           criteria.andDistrictIn(custom.getDistrictList());
       }
        example.setOrderByClause(String.format("id desc limit %s,%s", start, offset));
        return byUserWedBudgetMapper.selectByExample(example);
    }

    @Override
    public Integer selectIntentCustomListCount(ByUserWedBudget custom) {
        ByUserWedBudgetExample example = new ByUserWedBudgetExample();
        ByUserWedBudgetExample.Criteria criteria = example.createCriteria();
       /* if(userId != null){
            criteria.andUserIdEqualTo(userId);
        }*/
        return byUserWedBudgetMapper.countByExample(example);
    }

    @Override
    public Integer insertExample(ByUserExample byUserExample) {
        if (byUserExample.getId() !=null && byUserExample.getId() > 0){
            return byUserExampleMapper.updateByPrimaryKey(byUserExample);
        }else {
            return byUserExampleMapper.insert(byUserExample);
        }
    }

    @Override
    public ByUserExample findExample(Integer id) {
        return byUserExampleMapper.selectByPrimaryKey(id);
    }

    @Override
    public Integer deleteExample(Integer id) {
        return byUserExampleMapper.deleteByPrimaryKey(id);
    }

    @Override
    public List<ByUserExample> selectExampleListByPage(Integer userId, Integer status, Integer exampleType, Integer start, Integer offset) {
        ByUserExampleExample example = new ByUserExampleExample();
        ByUserExampleExample.Criteria criteria = example.createCriteria();
        if(userId != null){
            criteria.andUserIdEqualTo(userId);
        }
        if (status != null){
            criteria.andStatusEqualTo(status);
        }
        if (exampleType != null){
            criteria.andExampleTypeEqualTo(exampleType);
        }
        example.setOrderByClause(String.format("id desc limit %s,%s", start, offset));
        return byUserExampleMapper.selectByExample(example);
    }

    @Override
    public List<ByUserExampleBvo> selectExampleBvoListByPage(ByUserExampleBvo exampleBvo, RequestPage requestPage) {
        int total = byUserExampleMapper.countExampleBvoListByPage(exampleBvo,requestPage);
        requestPage.setTotal(total);
        return byUserExampleMapper.selectExampleBvoListByPage(exampleBvo,requestPage);
    }

    @Override
    public Integer selectExampleListCount(Integer userId, Integer status, Integer exampleType) {
        ByUserExampleExample example = new ByUserExampleExample();
        ByUserExampleExample.Criteria criteria = example.createCriteria();
        if(userId != null){
            criteria.andUserIdEqualTo(userId);
        }
        if (status != null){
            criteria.andStatusEqualTo(status);
        }
        if (exampleType != null){
            criteria.andExampleTypeEqualTo(exampleType);
        }
        return byUserExampleMapper.countByExample(example);
    }

    @Override
    public List<User> selectBizUserListByPage(Integer productType, Integer keyId,Integer channelId, String sortBy, String styleType, String price, String keyword, Integer start, Integer offset,
                                              Integer status, String city, String area, String orderBy) {

//        ByOpenCityExample cityExample = new ByOpenCityExample();
//        ByOpenCityExample.Criteria cityExampleCriteria = cityExample.createCriteria();
//        if(city != null){
//            cityExampleCriteria.andCityEqualTo(city);
//        }
//        if (area != null){
//            cityExampleCriteria.andAreaEqualTo(area);
//        }
//        List<ByOpenCity> openCities =  byOpenCityMapper.selectByExample(cityExample);
//        List<Integer> userIds = new ArrayList<Integer>();
//        if (openCities !=null && openCities.size() > 0) {
//            for (ByOpenCity current : openCities) {
//                userIds.add(current.getUserId());
//            }
//        }
//
//
        UserExample example = new UserExample();
        UserExample.Criteria criteria = example.createCriteria();
        List<Integer> idsList = null;
        if(keyId != null){
            List<User> userList = userMapper.findAllUserChannelByUserId(keyId,0,10000);
            idsList = new ArrayList<>();
            idsList.add(0);
            if(userList != null && userList.size() > 0){
                for(User user : userList){
                    idsList.add(user.getId());
                }
            }
        }
        if(idsList != null){
            criteria.andIdIn(idsList);
        }
//        if(channelId != null && channelId > 0){
//            List<Integer> workTypes = new ArrayList<Integer>();
//            workTypes.add(0);
//            workTypes.add(channelId);
//            criteria.andBizWorkTypeEqualTo(channelId);
//        }
//        if (userIds.size() > 0){
//            criteria.andIdIn(userIds);
//        }
        if(StringUtils.isNotBlank(city)){
            criteria.andCityEqualTo(city);
        }
        if(channelId != null && channelId > 0) {
            criteria.andBizWorkTypeEqualTo(channelId);
        }

        if (styleType != null && Integer.valueOf(styleType) > 0){
            criteria.andBizStyleLike("%"+styleType+"%");
        }
        if (status != null){
            criteria.andStatusEqualTo(status);
        }
//        List<Integer> userTypes = new ArrayList<Integer>();
//        userTypes.add(UserType.NORMAL_BIZ.getCode());
//        userTypes.add(UserType.TEAM_BIZ.getCode());
//
//        criteria.andUserTypeIn(userTypes);

        if (area != null && !area.equalsIgnoreCase("0")){
            criteria.andAreaEqualTo(area);
        }
        if (price != null && !"0".equals(price)) {
            if (price.indexOf("-") != -1) {
                String[] ptm = price.split("-");
                String reg = "[^0-9]";
                Pattern pattern = Pattern.compile(reg);
                Matcher matcher1 = pattern.matcher(ptm[0]);
                Matcher matcher2 = pattern.matcher(ptm[1]);
                String _tmp1 = matcher1.replaceAll("").trim();
                criteria.andBizPriceGreaterThanOrEqualTo(ObjectFormatter.str2BigDecimal(_tmp1));

                String _tmp2 = matcher2.replaceAll("").trim();
                if (Integer.valueOf(_tmp2) > 0) {
                    criteria.andBizPriceLessThan(ObjectFormatter.str2BigDecimal(_tmp2));
                }
            } else {
                String reg = "[^0-9]";
                Pattern pattern = Pattern.compile(reg);
                Matcher matcher = pattern.matcher(price);
                if (price.contains("以下")) {
                    String _tmp = matcher.replaceAll("").trim();
                    criteria.andBizPriceLessThan(ObjectFormatter.str2BigDecimal(_tmp));
                } else if (price.contains("以上")) {
                    criteria.andBizPriceGreaterThanOrEqualTo(ObjectFormatter.str2BigDecimal(matcher.replaceAll("").trim()));
                }
            }
        }

        if(StringUtils.isNotBlank(keyword)) {
            criteria.andCnNameLike("%" + keyword + "%");
        }

        //2
        UserExample.Criteria or_criteria_descn = example.createCriteria();
        if(idsList != null){
            or_criteria_descn.andIdIn(idsList);
        }
        if(StringUtils.isNotBlank(city)){
            or_criteria_descn.andCityEqualTo(city);
        }
        if(channelId != null && channelId > 0) {
            or_criteria_descn.andBizWorkTypeEqualTo(channelId);
        }

        if (styleType != null && Integer.valueOf(styleType) > 0){
            or_criteria_descn.andBizStyleLike("%"+styleType+"%");
        }
        if (status != null){
            or_criteria_descn.andStatusEqualTo(status);
        }

//        or_criteria_descn.andUserTypeIn(userTypes);

        if (area != null && !area.equalsIgnoreCase("0")){
            or_criteria_descn.andAreaEqualTo(area);
        }
        if (price != null && !"0".equals(price)) {
            if (price.indexOf("-") != -1) {
                String[] ptm = price.split("-");
                String reg = "[^0-9]";
                Pattern pattern = Pattern.compile(reg);
                Matcher matcher1 = pattern.matcher(ptm[0]);
                Matcher matcher2 = pattern.matcher(ptm[1]);
                String _tmp1 = matcher1.replaceAll("").trim();
                or_criteria_descn.andBizPriceGreaterThanOrEqualTo(ObjectFormatter.str2BigDecimal(_tmp1));

                String _tmp2 = matcher2.replaceAll("").trim();
                if (Integer.valueOf(_tmp2) > 0) {
                    or_criteria_descn.andBizPriceLessThan(ObjectFormatter.str2BigDecimal(_tmp2));
                }
            } else {
                String reg = "[^0-9]";
                Pattern pattern = Pattern.compile(reg);
                Matcher matcher = pattern.matcher(price);
                if (price.contains("以下")) {
                    String _tmp = matcher.replaceAll("").trim();
                    or_criteria_descn.andBizPriceLessThan(ObjectFormatter.str2BigDecimal(_tmp));
                } else if (price.contains("以上")) {
                    or_criteria_descn.andBizPriceGreaterThanOrEqualTo(ObjectFormatter.str2BigDecimal(matcher.replaceAll("").trim()));
                }
            }
        }

        if(StringUtils.isNotBlank(keyword)) {
            or_criteria_descn.andDescnLike("%" + keyword + "%");
        }

        example.or(or_criteria_descn);

        //3
        UserExample.Criteria or_criteria_tag = example.createCriteria();

        if(idsList != null){
            or_criteria_tag.andIdIn(idsList);
        }
        if(StringUtils.isNotBlank(city)){
            or_criteria_tag.andCityEqualTo(city);
        }
        if(channelId != null && channelId > 0) {
            or_criteria_tag.andBizWorkTypeEqualTo(channelId);
        }

        if (styleType != null && Integer.valueOf(styleType) > 0){
            or_criteria_tag.andBizStyleLike("%"+styleType+"%");
        }
        if (status != null){
            or_criteria_tag.andStatusEqualTo(status);
        }

//        or_criteria_tag.andUserTypeIn(userTypes);

        if (area != null && !area.equalsIgnoreCase("0")){
            or_criteria_tag.andAreaEqualTo(area);
        }
        if (price != null && !"0".equals(price)) {
            if (price.indexOf("-") != -1) {
                String[] ptm = price.split("-");
                String reg = "[^0-9]";
                Pattern pattern = Pattern.compile(reg);
                Matcher matcher1 = pattern.matcher(ptm[0]);
                Matcher matcher2 = pattern.matcher(ptm[1]);
                String _tmp1 = matcher1.replaceAll("").trim();
                or_criteria_tag.andBizPriceGreaterThanOrEqualTo(ObjectFormatter.str2BigDecimal(_tmp1));

                String _tmp2 = matcher2.replaceAll("").trim();
                if (Integer.valueOf(_tmp2) > 0) {
                    or_criteria_tag.andBizPriceLessThan(ObjectFormatter.str2BigDecimal(_tmp2));
                }
            } else {
                String reg = "[^0-9]";
                Pattern pattern = Pattern.compile(reg);
                Matcher matcher = pattern.matcher(price);
                if (price.contains("以下")) {
                    String _tmp = matcher.replaceAll("").trim();
                    or_criteria_tag.andBizPriceLessThan(ObjectFormatter.str2BigDecimal(_tmp));
                } else if (price.contains("以上")) {
                    or_criteria_tag.andBizPriceGreaterThanOrEqualTo(ObjectFormatter.str2BigDecimal(matcher.replaceAll("").trim()));
                }
            }
        }

        if(StringUtils.isNotBlank(keyword)) {
            or_criteria_tag.andTagNamesLike("%" + keyword + "%");
        }

        example.or(or_criteria_tag);
        if (orderBy != null && !"".equals(orderBy)) {
            example.setOrderByClause(String.format(orderBy + " limit %s,%s", start, offset));
        } else {
            example.setOrderByClause(String.format(" (SELECT count(1) from by_user_biz_auth a where u.id = a.user_id) desc limit %s,%s", start, offset));
        }
        return userMapper.selectByExample(example);
    }

    @Override
    public Integer selectBizUserListCount(Integer productType, Integer keyId,Integer channelId, String sortBy, String styleType, String price, String keyword,
                                          Integer status, String city, String area) {
//        ByOpenCityExample cityExample = new ByOpenCityExample();
//        ByOpenCityExample.Criteria cityExampleCriteria = cityExample.createCriteria();
//        if(city != null){
//            cityExampleCriteria.andCityEqualTo(city);
//        }
//        List<ByOpenCity> openCities =  byOpenCityMapper.selectByExample(cityExample);
//        List<Integer> userIds = new ArrayList<Integer>();
//        if (openCities !=null && openCities.size() > 0) {
//            for (ByOpenCity current : openCities) {
//                userIds.add(current.getUserId());
//            }
//        }

        //select
        UserExample example = new UserExample();
        UserExample.Criteria criteria = example.createCriteria();
//        if(channelId != null && channelId > 0){
//            List<Integer> workTypes = new ArrayList<Integer>();
//            workTypes.add(0);
//            workTypes.add(channelId);
//            criteria.andBizWorkTypeEqualTo(channelId);
//        }
//        if (userIds.size() > 0){
//            criteria.andIdIn(userIds);
//        }
        List<Integer> idsList = null;
        if(keyId != null){
            List<User> userList = userMapper.findAllUserChannelByUserId(keyId,0,10000);
            idsList = new ArrayList<>();
            idsList.add(0);
            if(userList != null && userList.size() > 0){
                for(User user : userList){
                    idsList.add(user.getId());
                }
            }
        }
        if(idsList != null){
            criteria.andIdIn(idsList);
        }
        if(StringUtils.isNotBlank(city)){
            criteria.andCityEqualTo(city);
        }
        if(channelId != null && channelId > 0) {
            criteria.andBizWorkTypeEqualTo(channelId);
        }

        if (styleType != null && Integer.valueOf(styleType) > 0){
            criteria.andBizStyleLike("%"+styleType+"%");
        }
        if (status != null){
            criteria.andStatusEqualTo(status);
        }
//        List<Integer> userTypes = new ArrayList<Integer>();
//        userTypes.add(UserType.NORMAL_BIZ.getCode());
//        userTypes.add(UserType.TEAM_BIZ.getCode());
//
//        criteria.andUserTypeIn(userTypes);

        if (area != null && !area.equalsIgnoreCase("0")){
            criteria.andAreaEqualTo(area);
        }
        if (price != null && !"0".equals(price)) {
            if (price.indexOf("-") != -1) {
                String[] ptm = price.split("-");
                String reg = "[^0-9]";
                Pattern pattern = Pattern.compile(reg);
                Matcher matcher1 = pattern.matcher(ptm[0]);
                Matcher matcher2 = pattern.matcher(ptm[1]);
                String _tmp1 = matcher1.replaceAll("").trim();
                criteria.andBizPriceGreaterThanOrEqualTo(ObjectFormatter.str2BigDecimal(_tmp1));

                String _tmp2 = matcher2.replaceAll("").trim();
                if (Integer.valueOf(_tmp2) > 0) {
                    criteria.andBizPriceLessThan(ObjectFormatter.str2BigDecimal(_tmp2));
                }
            } else {
                String reg = "[^0-9]";
                Pattern pattern = Pattern.compile(reg);
                Matcher matcher = pattern.matcher(price);
                if (price.contains("以下")) {
                    String _tmp = matcher.replaceAll("").trim();
                    criteria.andBizPriceLessThan(ObjectFormatter.str2BigDecimal(_tmp));
                } else if (price.contains("以上")) {
                    criteria.andBizPriceGreaterThanOrEqualTo(ObjectFormatter.str2BigDecimal(matcher.replaceAll("").trim()));
                }
            }
        }

        if(StringUtils.isNotBlank(keyword)) {
            criteria.andCnNameLike("%" + keyword + "%");
        }

        //2
        UserExample.Criteria or_criteria_descn = example.createCriteria();
        if(idsList != null){
            or_criteria_descn.andIdIn(idsList);
        }
        if(StringUtils.isNotBlank(city)){
            or_criteria_descn.andCityEqualTo(city);
        }
        if(channelId != null && channelId > 0) {
            or_criteria_descn.andBizWorkTypeEqualTo(channelId);
        }

        if (styleType != null && Integer.valueOf(styleType) > 0){
            or_criteria_descn.andBizStyleLike("%"+styleType+"%");
        }
        if (status != null){
            or_criteria_descn.andStatusEqualTo(status);
        }

//        or_criteria_descn.andUserTypeIn(userTypes);

        if (area != null && !area.equalsIgnoreCase("0")){
            or_criteria_descn.andAreaEqualTo(area);
        }
        if (price != null && !"0".equals(price)) {
            if (price.indexOf("-") != -1) {
                String[] ptm = price.split("-");
                String reg = "[^0-9]";
                Pattern pattern = Pattern.compile(reg);
                Matcher matcher1 = pattern.matcher(ptm[0]);
                Matcher matcher2 = pattern.matcher(ptm[1]);
                String _tmp1 = matcher1.replaceAll("").trim();
                criteria.andBizPriceGreaterThanOrEqualTo(ObjectFormatter.str2BigDecimal(_tmp1));

                String _tmp2 = matcher2.replaceAll("").trim();
                if (Integer.valueOf(_tmp2) > 0) {
                    criteria.andBizPriceLessThan(ObjectFormatter.str2BigDecimal(_tmp2));
                }
            } else {
                String reg = "[^0-9]";
                Pattern pattern = Pattern.compile(reg);
                Matcher matcher = pattern.matcher(price);
                if (price.contains("以下")) {
                    String _tmp = matcher.replaceAll("").trim();
                    criteria.andBizPriceLessThan(ObjectFormatter.str2BigDecimal(_tmp));
                } else if (price.contains("以上")) {
                    criteria.andBizPriceGreaterThanOrEqualTo(ObjectFormatter.str2BigDecimal(matcher.replaceAll("").trim()));
                }
            }
        }

        if(StringUtils.isNotBlank(keyword)) {
            or_criteria_descn.andDescnLike("%" + keyword + "%");
        }

        example.or(or_criteria_descn);

        //3
        UserExample.Criteria or_criteria_tag = example.createCriteria();
        if(idsList != null){
            or_criteria_tag.andIdIn(idsList);
        }
        if(StringUtils.isNotBlank(city)){
            or_criteria_tag.andCityEqualTo(city);
        }
        if(channelId != null && channelId > 0) {
            or_criteria_tag.andBizWorkTypeEqualTo(channelId);
        }

        if (styleType != null && Integer.valueOf(styleType) > 0){
            or_criteria_tag.andBizStyleLike("%"+styleType+"%");
        }
        if (status != null){
            or_criteria_tag.andStatusEqualTo(status);
        }

//        or_criteria_tag.andUserTypeIn(userTypes);

        if (area != null && !area.equalsIgnoreCase("0")){
            or_criteria_tag.andAreaEqualTo(area);
        }
        if (price != null && !"0".equals(price)) {
            if (price.indexOf("-") != -1) {
                String[] ptm = price.split("-");
                String reg = "[^0-9]";
                Pattern pattern = Pattern.compile(reg);
                Matcher matcher1 = pattern.matcher(ptm[0]);
                Matcher matcher2 = pattern.matcher(ptm[1]);
                String _tmp1 = matcher1.replaceAll("").trim();
                criteria.andBizPriceGreaterThanOrEqualTo(ObjectFormatter.str2BigDecimal(_tmp1));

                String _tmp2 = matcher2.replaceAll("").trim();
                if (Integer.valueOf(_tmp2) > 0) {
                    criteria.andBizPriceLessThan(ObjectFormatter.str2BigDecimal(_tmp2));
                }
            } else {
                String reg = "[^0-9]";
                Pattern pattern = Pattern.compile(reg);
                Matcher matcher = pattern.matcher(price);
                if (price.contains("以下")) {
                    String _tmp = matcher.replaceAll("").trim();
                    criteria.andBizPriceLessThan(ObjectFormatter.str2BigDecimal(_tmp));
                } else if (price.contains("以上")) {
                    criteria.andBizPriceGreaterThanOrEqualTo(ObjectFormatter.str2BigDecimal(matcher.replaceAll("").trim()));
                }
            }
        }

        if(StringUtils.isNotBlank(keyword)) {
            or_criteria_tag.andTagNamesLike("%" + keyword + "%");
        }

        example.or(or_criteria_tag);

        return userMapper.countByExample(example);
    }


    @Override
    public Integer insertExampleDetail(ByUserExampleDetail byUserExampleDetail) {
        if (byUserExampleDetail.getId() !=null && byUserExampleDetail.getId() > 0){
            return byUserExampleDetailMapper.updateByPrimaryKey(byUserExampleDetail);
        }else {
            return byUserExampleDetailMapper.insert(byUserExampleDetail);
        }
    }

    @Override
    public ByUserExampleDetail findExampleDetail(Integer id) {
        return byUserExampleDetailMapper.selectByPrimaryKey(id);
    }

    @Override
    public Integer deleteExampleDetail(Integer id) {
        return byUserExampleDetailMapper.deleteByPrimaryKey(id);
    }

    @Override
    public List<ByUserExampleDetail> selectExampleDetailListByPage(Integer userId, Integer exampleId, Integer status, Integer start, Integer offset) {
        ByUserExampleDetailExample example = new ByUserExampleDetailExample();
        ByUserExampleDetailExample.Criteria criteria = example.createCriteria();
        if(userId != null){
            criteria.andUserIdEqualTo(userId);
        }
        if(exampleId != null){
            criteria.andExampleIdEqualTo(exampleId);
        }
        if (status != null){
            criteria.andStatusEqualTo(status);
        }
        example.setOrderByClause(String.format("id desc limit %s,%s", start, offset));
        return byUserExampleDetailMapper.selectByExample(example);
    }

    @Override
    public Integer selectExampleDetailListCount(Integer userId, Integer exampleId, Integer status) {
        ByUserExampleDetailExample example = new ByUserExampleDetailExample();
        ByUserExampleDetailExample.Criteria criteria = example.createCriteria();
        if(userId != null){
            criteria.andUserIdEqualTo(userId);
        }
        if(exampleId != null){
            criteria.andExampleIdEqualTo(exampleId);
        }
        if (status != null){
            criteria.andStatusEqualTo(status);
        }
        return byUserExampleDetailMapper.countByExample(example);
    }



    @Override
    public Integer insertAgentCity(ByUserBizAgentCity byUserBizAgentCity) {
        if (byUserBizAgentCity.getId() !=null && byUserBizAgentCity.getId() > 0){
            return byUserBizAgentCityMapper.updateByPrimaryKey(byUserBizAgentCity);
        }else {
            return byUserBizAgentCityMapper.insert(byUserBizAgentCity);
        }
    }

    @Override
    public ByUserBizAgentCity findAgentCity(Integer id) {
        return byUserBizAgentCityMapper.selectByPrimaryKey(id);
    }

    @Override
    public Integer deleteAgentCity(Integer id) {
        return byUserBizAgentCityMapper.deleteByPrimaryKey(id);
    }

    @Override
    public List<ByUserBizAgentCity> selectAgentCityListByPage(Integer userId, Integer start, Integer offset) {
        ByUserBizAgentCityExample example = new ByUserBizAgentCityExample();
        ByUserBizAgentCityExample.Criteria cityExampleCriteria = example.createCriteria();
        if(userId != null){
            cityExampleCriteria.andUserIdEqualTo(userId);
        }

        example.setOrderByClause(String.format("id desc limit %s,%s", start, offset));
        return byUserBizAgentCityMapper.selectByExample(example);
    }

    @Override
    public Integer selectAgentCityListCount(Integer userId) {
        ByUserBizAgentCityExample example = new ByUserBizAgentCityExample();
        ByUserBizAgentCityExample.Criteria cityExampleCriteria = example.createCriteria();
        if(userId != null){
            cityExampleCriteria.andUserIdEqualTo(userId);
        }
        return byUserBizAgentCityMapper.countByExample(example);
    }


    @Override
    public Integer insertRecommendUser(ByUserRecommend byUserRecommend) {
        if (byUserRecommend.getId() !=null && byUserRecommend.getId() > 0){
            return byUserRecommendMapper.updateByPrimaryKey(byUserRecommend);
        }else {
            return byUserRecommendMapper.insert(byUserRecommend);
        }
    }

    @Override
    public ByUserRecommend findRecommendUser(Integer id) {
        return byUserRecommendMapper.selectByPrimaryKey(id);
    }

    @Override
    public Integer deleteRecommendUser(Integer id) {
        return byUserRecommendMapper.deleteByPrimaryKey(id);
    }

    @Override
    public List<ByUserRecommend> selectRecommendUserListByPage(Integer userId, Integer status, Integer exampleId, Integer start, Integer offset) {
        ByUserRecommendExample example = new ByUserRecommendExample();
        ByUserRecommendExample.Criteria criteria = example.createCriteria();
        if(userId != null){
            criteria.andUserIdEqualTo(userId);
        }
        if(status != null){
            criteria.andStatusEqualTo(status);
        }
        if(exampleId != null){
            criteria.andExampleIdEqualTo(exampleId);
        }else{
            criteria.andExampleIdIsNull();
        }
        example.setOrderByClause(String.format("sort desc, id desc limit %s,%s", start, offset));
        return byUserRecommendMapper.selectByExample(example);
    }

    @Override
    public List<ByUserRecommendBvo> selectByUserRecommendBvoListByPage(Integer userId, Integer status, Integer exampleId,RequestPage page) {
        int total = byUserRecommendMapper.countByUserRecommendBvoListByPage(userId,status,exampleId,page);
        page.setTotal(total);
        return byUserRecommendMapper.selectByUserRecommendBvoListByPage(userId,status,exampleId,page);
    }

    @Override
    public Integer selectRecommendUserListCount(Integer userId, Integer status, Integer exampleId) {
        ByUserRecommendExample example = new ByUserRecommendExample();
        ByUserRecommendExample.Criteria criteria = example.createCriteria();
        if(userId != null){
            criteria.andUserIdEqualTo(userId);
        }
        if(status != null){
            criteria.andStatusEqualTo(status);
        }
        if(exampleId != null){
            criteria.andExampleIdEqualTo(exampleId);
        }else{
            criteria.andExampleIdIsNull();
        }
        return byUserRecommendMapper.countByExample(example);
    }

    @Override
    public Integer deleteByExampleBudgetKey(Integer id) {
        return byExampleBudgetMapper.deleteByPrimaryKey(id);
    }

    @Override
    public ByExampleBudget selectByExampleBudgetKey(Integer id) {
        return byExampleBudgetMapper.selectByPrimaryKey(id);
    }

    @Override
    public Integer insertExampleBudget(ByExampleBudget byExampleBudget) {
        if (byExampleBudget.getId() !=null && byExampleBudget.getId() > 0){
            return byExampleBudgetMapper.updateByPrimaryKey(byExampleBudget);
        }else {
            return byExampleBudgetMapper.insert(byExampleBudget);
        }
    }

    @Override
    public Integer updateByExampleBudgetKey(ByExampleBudget byExampleBudget) {
        return byExampleBudgetMapper.updateByPrimaryKey(byExampleBudget);
    }

    @Override
    public List<ByExampleBudget> selectExampleBudgetListByPage(Integer exampleId, RequestPage page) {
        int total = byExampleBudgetMapper.countExampleBudgetListByPage(exampleId,page);
        page.setTotal(total);
        return byExampleBudgetMapper.selectExampleBudgetListByPage(exampleId,page);
    }

    @Override
    public Integer deleteByExampleDiscussKey(Integer id) {
        return byExampleDiscussMapper.deleteByPrimaryKey(id);
    }

    @Override
    public ByExampleDiscuss selectByExampleDiscussKey(Integer id) {
        return byExampleDiscussMapper.selectByPrimaryKey(id);
    }

    @Override
    public Integer insertExampleDiscuss(ByExampleDiscuss byExampleDiscuss) {
        if (byExampleDiscuss.getId() !=null && byExampleDiscuss.getId() > 0){
            return byExampleDiscussMapper.updateByPrimaryKey(byExampleDiscuss);
        }else {
            return byExampleDiscussMapper.insert(byExampleDiscuss);
        }
    }

    @Override
    public Integer updateByExampleDiscussKey(ByExampleDiscuss byExampleDiscuss) {
        return byExampleDiscussMapper.updateByPrimaryKey(byExampleDiscuss);
    }

    @Override
    public List<ByExampleDiscuss> selectExampleDiscussListByPage(Integer exampleId, RequestPage page) {
        int total = byExampleDiscussMapper.countExampleDiscussListByPage(exampleId,page);
        page.setTotal(total);
        return byExampleDiscussMapper.selectExampleDiscussListByPage(exampleId,page);
    }

    @Override
    public Integer deleteByDictKey(Integer id) {
        return dictMapper.deleteByPrimaryKey(id);
    }

    @Override
    public Dict selectByDictKey(Integer id) {
        return dictMapper.selectByPrimaryKey(id);
    }

    @Override
    public Integer insertDict(Dict record) {
        if (record.getId() !=null && record.getId() > 0){
            return dictMapper.updateByPrimaryKey(record);
        }else {
            return dictMapper.insert(record);
        }
    }

    @Override
    public Integer updateByDictKey(Dict dict) {
        return dictMapper.updateByPrimaryKey(dict);
    }

    @Override
    public List<Dict> selectDictListByPage(Integer type, RequestPage page) {
        int total = dictMapper.countDictListByPage(type,page);
        page.setTotal(total);
        return dictMapper.selectDictListByPage(type,page);
    }

    @Override
    public Integer insertUserMessage(ByUserMessage userMessage) {
        return userMessageMapper.insert(userMessage);
    }

    @Override
    public List<ByUserMessage> selectUserMessageByPage(Integer userId, RequestPage page) {
        int total = userMessageMapper.countByUserMessageListByPage(userId,page);
        page.setTotal(total);
        return userMessageMapper.selectByUserMessageListByPage(userId,page);
    }

    @Override
    public Integer deleteByUserMessageKey(Integer id) {
        return userMessageMapper.deleteByPrimaryKey(id);
    }

    @Override
    public List<ByUserBizAuth> findUserBizAuthByUserId(int userId) {
        return byUserBizAuthMapper.findUserBizAuthByUserId(userId);
    }
}
